import { _pick } from "mmb";
import { AppCTX } from "../../types/koaExtend"
import { listUserFollowWithUserAndUserStats } from "../../service/userFollowService"
import { listBrandFollowWithBrandAndBrandStats } from "../../service/brandFollowService"
import { listStoreFollowWithStoreAndStoreStats } from "../../service/storeFollowService"
import pageMW from "../../middleware/pager";
import Router from "koa-router";
const router = new Router();

// // 获取指定用户的关注列表
// router.get("user-followed-objects", '/usr/user/:userId/followedObjects', pageMW, async(ctx: AppCTX, next) => {
//   const {userId} = ctx

//   ctx.body = {
//     userId,
//     followedObjects:[{
//       objectType: 'brand',
//       name: '品牌名称',
//       fans_count: 2,
//       pub_activity_count: 4 // 品牌是发布的活动数
//     }, {
//       objectType: 'user',
//       name: '用户名称',
//       fans_count: 2,
//       pub_activity_count: 4 // 用户发布代言的活动数
//     }]
//   }
// })

// // 获取用户的关注对象列表
// router.get("/usr/followedObjects", (ctx: AppCTX) => {
//   const { userId } = ctx;
//   const url = router.url("user-followed-objects", { userId }, { query: ctx.query });
//   ctx.redirect(url);
//   ctx.status = 301;
// });

// 获取指定用户的关注用户列表
router.get("user-followed-users", '/usr/user/:userId/followedUsers', pageMW, async(ctx: AppCTX, next) => {
  const {userId, limit, offset } = ctx
  const {total, items} = await listUserFollowWithUserAndUserStats(userId, {limit, offset})
  ctx.body = {
    total,
    rows: items.map((it) => ({
      followedObjectType: 'user',
      followedObjectId: it.followedUserId,
      followedObjectName: it.followedUserName,
      followedObjectPic: it.followedUserPic,
      fansCount: it.fansCount,
      activityCount: it.endorseActivityCount,
    }))
  }
})

// 获取用户的关注用户列表
router.get("/usr/followedUsers", (ctx: AppCTX) => {
  const { userId } = ctx;
  const url = router.url("user-followed-users", { userId }, { query: ctx.query });
  ctx.redirect(url);
  ctx.status = 301;
});

// 获取指定用户的关注品牌列表
router.get("user-followed-brands", '/usr/user/:userId/followedBrands', pageMW, async(ctx: AppCTX, next) => {
  const {userId, limit, offset } = ctx
  const {total, items} = await listBrandFollowWithBrandAndBrandStats(userId, {limit, offset})
  ctx.body = {
    total,
    rows: items.map((it) => ({
      followedObjectType: 'brand',
      followedObjectId: it.followedBrandId,
      followedObjectName: it.followedBrandName,
      followedObjectPic: it.followedBrandPic,
      fansCount: it.fansCount,
      activityCount: it.publishActivityCount,
    }))
  }
})

// 获取用户的关注品牌列表
router.get("/usr/followedBrands", (ctx: AppCTX) => {
  const { userId } = ctx;
  const url = router.url("user-followed-brands", { userId }, { query: ctx.query });
  ctx.redirect(url);
  ctx.status = 301;
});

// 获取指定用户的关注门店列表
router.get("user-followed-stores", '/usr/user/:userId/followedStores', pageMW, async(ctx: AppCTX, next) => {
  const {userId, limit, offset } = ctx
  const {total, items} = await listStoreFollowWithStoreAndStoreStats(userId, {limit, offset})
  ctx.body = {
    total,
    rows: items.map((it) => ({
      followedObjectType: 'brand',
      followedObjectId: it.followedStoreId,
      followedObjectName: it.followedStoreName,
      followedObjectPic: it.followedStorePic,
      fansCount: it.fansCount,
      activityCount: it.publishActivityCount,
    }))
  }
})

// 获取用户的关注门店列表
router.get("/usr/followedStores", (ctx: AppCTX) => {
  const { userId } = ctx;
  const url = router.url("user-followed-stores", { userId }, { query: ctx.query });
  ctx.redirect(url);
  ctx.status = 301;
});


export default router